<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>添加菜单页面</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
        <link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
        <link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
        <link rel="stylesheet" href="${ctx}/css/public.css">
        <script type="text/javascript">
            var __ctx = "${ctx}";
        </script>
    </head>
    <body>
        <div style="position: relative;height: 100%;padding: 10px">
            <table id="fileManagementList" lay-filter="fileManagementList"></table>
        </div>
        <!-- 头部工具栏 -->
        <script type="text/html" id="headerToolsBarRole">
            <div class="ccpui-enterSel">
                <div class="layui-btn-container fileheadertool">
                    <a class="layui-btn layui-btn-sm" lay-event="query">
                        <i class="layui-icon">&#xe615;</i>
                        <span>查询</span>
                    </a>
                    <a class="layui-btn layui-btn-sm" lay-event="clears">
                        <i class="layui-icon">&#xe9aa;</i>
                        <span>重置</span>
                    </a>
                    <#if parentType =='1'>
                    <a class="layui-btn layui-btn-sm" lay-event="delete">
                        <i class="layui-icon">&#xe640;</i>
                        <span>删除</span>
                    </a>
                </#if>
                <#if managePermission.addMapping==true && parentType == '1'>
                <a class="layui-btn layui-btn-sm" lay-event="fileMapping">
                    <i class="layui-icon layui-icon-engine"></i> <span>文件映射</span>
                </a>
            </#if>
            <#if managePermission.cancelMapping==true && parentType == '2'>
            <a class="layui-btn layui-btn-sm" lay-event="canfileMapping">
                <i class="layui-icon layui-icon-fonts-del"></i> <span>取消文件映射</span>
            </a>
            </#if>
            <#if managePermission.addFile==true && parentType == '1'>
            <a class="layui-btn layui-btn-sm" lay-event="fileUpload">
                <i class="layui-icon layui-icon-upload-drag"></i> <span>上传文件</span>
            </a>
            </#if>
            <#if managePermission.download==true>
            <a class="layui-btn layui-btn-sm" lay-event="topdownloadFiles">
                <i class="layui-icon layui-icon-download-circle"></i> <span>下载</span>
            </a>
            </#if>
            </div>
            <form class="layui-form" action="" lay-filter="fileSelect" style="margin-top:10px">
                <div class="table-top-filter">
                    <div class="table-filter-alone">
                        <label class="layui-form-label">文件名称</label>
                        <input type="text" class="layui-input  filter-value" name="Q_fileName_SL"/>
                    </div>
                    <div class="table-filter-alone">
                        <label class="layui-form-label">摘要</label>
                        <input type="text" class="layui-input  filter-value" name="Q_fileKeywords_SL"/>
                    </div>
                </div>
            </form>
            </div>
        </script>

        <!--编辑功能-->
        <div class="fileManagement-editFolder">
            <div class="layui-form">
                <div class="layui-form-item" style="margin-top: 15px">
                    <label class="layui-form-label">文件夹名称</label>
                    <div class="layui-input-inline" style="width: 310px">
                        <input type="text" name="fileName" lay-verify="required" autocomplete="off"  class="layui-input editfolderfileName">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <input type="button" lay-submit lay-filter="editFolder-submit" id="editFolderSubmit">
                </div>
            </div>
        </div>

        <!-- 右侧工具栏 -->
        <script type="text/html" id="rightToolsBarRole"></script>
        <script src="${ctx}/public/component.js" type="text/javascript"></script>
        <script src="${ctx}/layui/layui.js"></script>
        <script>
            layui.config({
                base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
            }).extend({
                index: 'index' //主入口模块
            }).use(['index', 'setter', 'commons', 'form', 'jquery', 'layer', 'common', 'table', 'totalFilter'], function () {
                var table = layui.table, layer = layui.layer, setter = layui.setter, commons = layui.commons,
                    common = layui.common, totalFilter = layui.totalFilter, form = layui.form, $ = layui.jquery;


                var globalVriable={//全局变量集合
                    parentType:'${parentType}' //初始化treetype参数  1物理树目录节点 2逻辑归类树目录节点
                    ,nowTreeNodeid:'${parentId}' //当前单击树节点id
                };

                //列表右侧按钮权限
                function btnmanagePermission(data) {
                    $('#rightToolsBarRole').html('');
                    if(data&&data.edit){
                        $('#rightToolsBarRole').append(' <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>')
                    }
                    if(globalVriable.parentType=='1'){
                        $('#rightToolsBarRole').append('<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>')
                    }
                    if(data&&data.download){
                        $('#rightToolsBarRole').append('<a class="layui-btn layui-btn-xs" lay-event="downloadfile">下载</a>')
                    }
                }

                //判断文件类型  选择图标
                function fileTypeIcon(val) {
                    var html='';
                    switch (val) {
                        case 'docx':
                            html='<img src="${ctx}/systemStatic/images/docx.gif" width="18px">';
                            break;
                        case 'doc':
                            html='<img src="${ctx}/systemStatic/images/docx.gif" width="18px">';
                            break;
                        case 'mp3':
                            html='<img src="${ctx}/systemStatic/images/mp3.gif" width="18px">';
                            break;
                        case 'mp4':
                            html='<img src="${ctx}/systemStatic/images/mp4.gif" width="18px">';
                            break;
                        case 'photo':
                            html='<img src="${ctx}/systemStatic/images/photo.gif" width="18px">';
                            break;
                        case 'ppt':
                            html='<img src="${ctx}/systemStatic/images/ppt.gif" width="18px">';
                            break;
                        case 'xls':
                            html='<img src="${ctx}/systemStatic/images/xls.gif" width="18px">';
                            break;
                        case 'xlsx':
                            html='<img src="${ctx}/systemStatic/images/xls.gif" width="18px">';
                            break;
                        case 'zip':
                            html='<img src="${ctx}/systemStatic/images/zip.gif" width="18px">';
                            break;
                        case 'txt':
                            html='<img src="${ctx}/systemStatic/images/txt.gif" width="18px">';
                            break;
                        default:
                            html='<img src="${ctx}/systemStatic/images/unKnow.gif" width="18px">';
                    }
                    return html;
                }


                var tableHeight = commons.tabHeight();
                function toggleTable(datajson){
                    var returnbtnData='';//按钮权限
                    var tableCols=[{type: 'checkbox'},
                        {field: 'id', title: 'ID', sort: true, hide: true},
                        {field: 'fileName',event: 'fileName', align: "center", title: '文件名称', sort: true,sortname:'file_name',
                            templet: function (d) {
                                if(d.fileType=='1'){
                                    var fileNameA = "<span style='cursor: pointer; color: #0b73df;'>"+d.fileName+"</span>"
                                    return fileNameA
                                }
                                var imghtml=fileTypeIcon(d.fileFormat);
                                return imghtml+' '+d.fileName+'.'+d.fileFormat;
                            }},
                        {field: 'fileSecretLevel', align: "center", title: '文件密级',sort: true,sortname:'file_secret_level',templet: function (d) {return d.fileSecretStr;}},
                        {field: 'fileKeywords', title: '摘要', align: "center",sort: true,sortname:'file_keywords'},
                        {field: 'fileSize', align: "center", title: '大小', sort: true,sortname:'file_size'},
                        {field: 'uploadPersonName', align: "center", title: '上传人', sort: true,sortname:'upload_person_name'},
                        {field: 'uploadDate', align: "center", title: '上传时间',templet: function(d) {return component.timeData(d.uploadDate);}, sort: true,sortname:'upload_date'},
                        {align: 'center', title: '管理', toolbar: '#rightToolsBarRole'}
                    ];
                    if(datajson&&datajson.parentType==2){
                        tableCols.splice(3, 0, {field: 'alias', align: "center", title: '映射文件别名', sort: true});
                        tableCols.splice(4, 0, {field: 'orderNumber', align: "center", title: '排序编号',sortname:'order_number'});
                    }
                    table.render({
                        elem: '#fileManagementList',
                        cellMinWidth: 80,
                        title: '文件信息列表',
                        height: tableHeight,
                        defaultToolbar: ['filter', 'print'],
                        url: serverPath.systemPath + setter.sysLinkPath + "fileManagement/fileManagementList",
                        page: true,
                        method: 'get',
                        where:datajson,
                        toolbar: '#headerToolsBarRole',
                        cols:[tableCols],
                        parseData: function(res){ //res 即为原始返回的数据
                            btnmanagePermission(res.data.managePermission);
                            returnbtnData=res.data.managePermission;
                            return {
                                "code": res.code, //解析接口状态
                                "msg": res.msg, //解析提示文本
                                "count": res.count, //解析数据长度
                                "data": res.data.fileManagementList //解析数据列表
                            };
                        }
                        ,done: function () {
                            common.tableSelectCon('.ccpui-enterSel .layui-form input', 'a[lay-event=query]', '.ccpui-enterSel')
                        }
                    });
                }
                toggleTable({parentType:globalVriable.parentType,"parentId": globalVriable.nowTreeNodeid});//初始化表
                totalFilter.init('fileManagementList');
                //监听头工具栏事件
                table.on('toolbar(fileManagementList)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    var objData = checkStatus.data;
                    switch (obj.event) {
                        case 'delete':
                            if (objData.length === 0) {
                                layer.msg('请先选择一行数据！');
                            } else {
                                layer.confirm('确定删除当前数据吗？', function () {
                                    var seleIds = [];
                                    for (var i = 0; i < objData.length; i++) {
                                        seleIds.push(objData[i].id);
                                    }
                                    $.ajax({
                                        url: serverPath.systemPath + setter.sysLinkPath + "fileManagement/deleteFile",
                                        type: "post",
                                        dataType: 'json',
                                        data: {fileIds: seleIds.join(",")},
                                        success: function (res) {
                                            layer.alert(res.msg);
                                            if(res.code=='0'){
                                                table.reload('fileManagementList', {
                                                    page: {curr: 1, limit: 10},
                                                });
                                                totalFilter.init('fileManagementList');
                                            }
                                        }
                                    })
                                })
                            }
                            break;
                        case 'query':
                            common.tableConditionRender("fileManagementList", "fileSelect", {where: {}}, false);
                            break;
                        case 'clears':
                            common.tableConditionRender("fileManagementList", "fileSelect", {where: {}}, true);
                            break;
                        case 'fileMapping':
                            if (objData.length === 0) {
                                layer.msg('请选择文件！',{icon: 0});
                            } else {
                                var body;
                                var userSelectorWin;
                                var theFileIds = [];
                                var isFile = true;
                                $.each(objData,function (key,val) {
                                    if (val.fileType!==2){
                                        isFile = false;
                                    }else {
                                        theFileIds.push(val.id)
                                    }
                                })
                                if (isFile){
                                    layer.open({
                                        type: 2,
                                        area: ['400px', '500px'],
                                        btn:['保存','取消'],
                                        title: ['文件映射'],
                                        content: serverPath.systemPath + setter.sysLinkPath + 'fileManagement/view/mapping',
                                        success: function(layero, index){
                                            body = layer.getChildFrame('body',index);
                                            userSelectorWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                                        },
                                        yes:function (index, layero) {
                                            if(body.find("input[name=treeLj]").val()!==""){
                                                var dataMapping = {
                                                    fileIds:theFileIds.toString(),
                                                    alias:objData[0].fileName,
                                                    parentId: body.find("input[name=treeLj]").val()
                                                }
                                                $.ajax({
                                                    url:serverPath.systemPath + setter.sysLinkPath + "fileManagement/addMapping",
                                                    type: "post",
                                                    dataType: 'json',
                                                    data:dataMapping,
                                                    success: function (res) {
                                                        if (res.code==0){
                                                            if(res.data==""){
                                                                layer.close(index); //再执行关闭
                                                                layer.msg(res.msg,{icon: 6});
                                                            }else {
                                                                layer.msg(res.data,{icon: 0});
                                                            }

                                                        }
                                                    }
                                                });
                                            }else {
                                                layer.msg('请选择目录！',{icon: 0});
                                            }
                                        },
                                        btn2:function (index, layero) {
                                            layer.close(index); //再执行关闭
                                        }
                                    });
                                }else {
                                    layer.msg("不能选择文件夹！",{icon: 0});
                                }
                            }
                            break;
                        case 'fileUpload':
                            events.addFiles();
                            break;
                        case 'topdownloadFiles':
                            if (objData.length === 0) {
                                layer.msg('请先选择一行数据！');
                            } else {
                                layer.confirm('确定下载选中文件吗？', function (index) {
                                    layer.close(index);
                                    var seleIds = [];
                                    for (var i = 0; i < objData.length; i++) {
                                        seleIds.push(objData[i].id);
                                    }
                                    var url = serverPath.systemPath + setter.sysLinkPath + 'fileManagement/downloadFile?fileIds='+ seleIds.join(",");
                                    window.location.href = url;
                                })
                            }
                            break
                        case 'canfileMapping':
                            if (objData.length === 0) {
                                layer.msg('请先选择一行数据！',{icon: 3});
                            } else {
                                layer.confirm('确认取消映射吗?',{icon: 3, title:'提示'}, function(index){
                                    //do something
                                    var theFileIds = [];
                                    $.each(objData,function (key,val) {
                                        theFileIds.push(val.id)
                                    })
                                    $.ajax({
                                        url:serverPath.systemPath + setter.sysLinkPath + "fileManagement/cancelMapping",
                                        type: "post",
                                        dataType: 'json',
                                        data:{fileIds:theFileIds.toString()},
                                        success: function (res) {
                                            if (res.code==0){
                                                layer.close(index); //再执行关闭
                                                table.reload('fileManagementList');
                                                layer.msg(res.msg,{icon: 6});
                                            }
                                        }
                                    });
                                });
                            }

                            break
                    }
                });

                //监听行工具事件
                table.on('tool(fileManagementList)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'del':
                            layer.confirm('确定删除当前数据吗？', function (index) {
                                layer.close(index);
                                $.ajax({
                                    url: serverPath.systemPath + setter.sysLinkPath + "fileManagement/deleteFile",
                                    type: "post",
                                    dataType: 'json',
                                    data: {fileIds: obj.data.id},
                                    success: function (res) {
                                        layer.alert(res.msg);
                                        if(res.code=='0'){
                                            table.reload('fileManagementList', {
                                                page: {curr: 1, limit: 10},
                                            });
                                            totalFilter.init('fileManagementList');
                                        }
                                    }
                                })
                            });
                            break;
                        case 'edit':
                            if(obj.data.fileType=='1'){
                                events.editFile(obj)
                            }else{
                                layer.open({
                                    type: 2,
                                    area:(globalVriable.parentType=='1'? ['560px', '550px']:['560px', '660px']),
                                    title: ['文件修改'],
                                    content:  serverPath.systemPath + setter.sysLinkPath + 'fileManagement/view/edit?fileId='+obj.data.id,
                                    btn: ['保存','取消'],
                                    yes:function (index, layero) {
                                        var iframeWindow = window['layui-layer-iframe'+ index]
                                            ,submit = layero.find('iframe').contents().find("#fileManagement-edit-form-submit");

                                        //监听提交
                                        iframeWindow.layui.form.on('submit(fileManagement-edit-form-submit)', function(datas){
                                            datas.field.id=obj.data.id;
                                            $.ajax({
                                                url:serverPath.systemPath + setter.sysLinkPath + "fileManagement/editFile",
                                                type: "post",
                                                contentType: 'application/json',
                                                dataType: 'json',
                                                data:JSON.stringify(datas.field),
                                                success: function (res) {
                                                    layer.alert(res.msg,{closeBtn: 0},function (index1) {
                                                        layer.close(index1);
                                                        if(res.code==0){
                                                            table.reload('fileManagementList', {
                                                                page: {curr: 1, limit: 10},
                                                            });
                                                            totalFilter.init('fileManagementList');
                                                            layer.close(index); //关闭弹层
                                                        }
                                                    })
                                                }
                                            });
                                        });
                                        submit.trigger('click');
                                    }
                                });
                            }
                            break;
                        case 'downloadfile':
                            layer.confirm('确定下载吗？', {}, function (index) {
                                layer.close(index);
                                var url = serverPath.systemPath + setter.sysLinkPath + 'fileManagement/downloadFile?fileIds='+obj.data.id;
                                window.location.href = url;
                            });
                            break;
                        case 'fileName':
                            if (data.fileType==2){
                                return
                            }else {
                                common.tableConditionRender("fileManagementList", "fileSelect", {where: {"parentId": data.id,parentType:data.parentType}}, true);
                            }
                            break;
                    }
                });
                common.tableSort('fileManagementList');

                var events={
                    addFiles:function () {//上传
                        if(globalVriable.nowTreeNodeid){
                            layer.open({
                                type: 2,
                                area: ['800px', '585px'],
                                title: ['上传文件'],
                                content: serverPath.systemPath + setter.sysLinkPath + 'fileManagement/view/file?parentId='+globalVriable.nowTreeNodeid,
                                btn: ['保存', '取消'],
                                skin: 'btn-class addFiles-class',
                                yes: function(index, layero){
                                    var iframeWindow = window['layui-layer-iframe'+ index]
                                        ,submit = layero.find('iframe').contents().find("#fileManagement-file-form-submit");
                                    submit.trigger('click');
                                },
                                end:function(){
                                    table.reload('fileManagementList', {
                                        page: {curr: 1, limit: 10},
                                    });
                                    totalFilter.init('fileManagementList');
                                }
                            });
                        }
                    }

                    ,editFile:function (nodedata) {//编辑
                        layer.open({
                            type: 1,
                            area: ['460px', '180px'],
                            title: ['编辑文件夹'],
                            content: $('.fileManagement-editFolder'),
                            btn: ['保存','取消'],
                            success:function(layero, index){
                                $('input[name="fileName"].editfolderfileName').val(nodedata.data.fileName);
                                form.on('submit(editFolder-submit)', function(data){
                                    data.field.id=nodedata.data.id;
                                    $.ajax({
                                        url:serverPath.systemPath + setter.sysLinkPath + "fileManagement/editFolder",
                                        type: "post",
                                        contentType: 'application/json',
                                        dataType: 'json',
                                        data:JSON.stringify(data.field),
                                        success: function (res) {
                                            layer.alert(res.msg,{closeBtn: 0},function (index1) {
                                                layer.close(index1);
                                                if(res.code==0){
                                                    nodedata.update({
                                                        fileName: data.field.fileName
                                                    });
                                                    layer.close(index); //再执行关闭
                                                }
                                            })
                                        }
                                    });
                                });
                            },
                            yes:function (index, layero) {
                                layero.find('#editFolderSubmit').click();
                            },
                            // btn2:function (index, layero) {
                            //     layer.close(index); //再执行关闭
                            // },
                            end:function () {
                                $('input[name="fileName"].editfolderfileName').val('');
                            }
                        });
                    }
                };

                //时间点击
                $('body').on('click', '*[fileManage-event]', function(e){
                    var othis = $(this), methid = othis.attr('fileManage-event');
                    events[methid] ? events[methid].call(this, othis, e) : '';
                });
            });
        </script>
    </body>
</html>
<link rel="stylesheet" href="${ctx}/css/font/iconfontie.css" media="all"/>
<link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>